﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Globalization;
using System.Xml;

namespace Nuane.Posterous
{
	/// <summary>
	/// Information about a single posterous site (blog).
	/// </summary>
	public class PosterousSite
	{
		//<site>  
		//  <id>1</id>  
		//  <name>Sachin Agarwal's Posterous</name>  
		//  <url>http://sachin.posterous.com</url>  
		//  <private>false</private>  
		//  <primary>true</primary>  
		//  <commentsenabled>true</commentsenabled>  
		//  <num_posts>50</num_posts>  
		//</site>

		private readonly PosterousSession _session;

		/// <summary>
		/// Gets site ID.
		/// </summary>
		/// <value>Site ID.</value>
		public int Id { get; private set; }

		/// <summary>
		/// Gets site name.
		/// </summary>
		/// <value>Site name.</value>
		public string Name { get; private set; }

		/// <summary>
		/// Gets site URI.
		/// </summary>
		/// <value>Site URI.</value>
		public Uri Uri { get; private set; }

		/// <summary>
		/// Gets site hostname.
		/// </summary>
		/// <value>Site hostname</value>
		public string Hostname
		{
			get
			{
				return Uri.Host;
			}
		}

		/// <summary>
		/// Gets a value indicating whether the site is private.
		/// </summary>
		/// <value>True if private; false if not private.</value>
		public bool IsPrivate { get; private set; }

		/// <summary>
		/// Gets a value indicating whether this site is primary.
		/// </summary>
		/// <value>True if primary; false if not primary.</value>
		public bool IsPrimary { get; private set; }

		/// <summary>
		/// Gets a value indicating whether comments are enabled for this site.
		/// </summary>
		/// <value>True if enabled; false if disabled.</value>		
		public bool CommentsEnabled { get; private set; }

		/// <summary>
		/// Gets the number of posts in this site.
		/// </summary>
		/// <value>Number of posts.</value>
		public int PostsCount { get; private set; }

		internal PosterousSite(PosterousSession session, XmlNode site)
		{
			_session = session;

			if (site.Name != "site")
				throw new PosterousException("Invalid response.", PosterousExceptionStatus.ServerProtocolViolation);

			Id = Helper.ToInt32(Helper.GetChildElementValue(site, "id"));
			Name = Helper.GetChildElementValue(site, "name");
			Uri = Helper.ToUri(Helper.GetChildElementValue(site, "url"));
			IsPrivate = Helper.ToBoolean(Helper.GetChildElementValue(site, "private"));
			IsPrimary = Helper.ToBoolean(Helper.GetChildElementValue(site, "primary"));
			CommentsEnabled = Helper.ToBoolean(Helper.GetChildElementValue(site, "commentsenabled"));
			PostsCount = Helper.ToInt32(Helper.GetChildElementValue(site, "num_posts"));
		}

		/// <summary>
		/// Gets site name.
		/// </summary>
		/// <returns>Site name.</returns>
		public override string ToString()
		{
			return Name;
		}

		/// <summary>
		/// Publishes the specified post.
		/// </summary>
		/// <param name="newPost">New post.</param>
		/// <returns>The new post info.</returns>
		public PosterousPostInfo PublishPost(PosterousNewPost newPost)
		{
			return _session.PublishPost(Id, newPost);
		}

		/// <summary>
		/// Publishes the specified post.
		/// </summary>
		/// <param name="title">Title. Cannot be null.</param>
		/// <param name="body">Body. May be null.</param>
		/// <returns>The new post (only partially initialized if private).</returns>
		public PosterousPostInfo PublishPost(string title, string body)
		{
			return _session.PublishPost(Id, title, body);
		}

		/// <summary>
		/// Get posts from the specified site.
		/// </summary>
		/// <param name="offset">Offset of the first post (zero-based).</param>
		/// <param name="count">Number of posts to retrieve.</param>
		/// <param name="tag">Tag.</param>
		/// <returns>Collection of posts.</returns>
		public PosterousPostCollection GetPosts(int offset, int count, string tag)
		{
			return _session.GetPosts(Id, offset, count, tag);
		}

		/// <summary>
		/// Get posts from the specified site.
		/// </summary>
		/// <param name="offset">Offset of the first post (zero-based).</param>
		/// <param name="count">Number of posts to retrieve.</param>
		/// <returns>Collection of posts.</returns>
		public PosterousPostCollection GetPosts(int offset, int count)
		{
			return _session.GetPosts(Id, offset, count, null);
		}
	}
}
